package com.tianyi.pictureAlbum;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tianyi.dao.entity.pictureAlbum.Picturealbum;
import com.tianyi.dao.entity.pictureAlbum.PicturealbumExample;
import com.tianyi.dao.mapper.PicturealbumMapper;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.PictureAlbumReq;
import com.tianyi.lib.req.QueryPage;
import com.tianyi.menu.MenuService;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@Service
public class PictureAlbumService {

    private static Logger logger = LoggerFactory.getLogger(MenuService.class);

    @Autowired
    private PicturealbumMapper picturealbumMapper;

    public WebResult<?> addPictureAlbum(PictureAlbumReq pictureAlbumReq){
        Picturealbum picturealbum = new Picturealbum();
        picturealbum.setAlbumdescription(pictureAlbumReq.getAlbumdescription());
        picturealbum.setAlbumname(pictureAlbumReq.getAlbumname());
        picturealbum.setContactperson(pictureAlbumReq.getContactperson());
        picturealbum.setCreatoruser(pictureAlbumReq.getCreatoruser());
        picturealbum.setRelatedproducts(pictureAlbumReq.getRelatedproducts());
        picturealbum.setShelfmanagement(pictureAlbumReq.getShelfmanagement());
        picturealbum.setShelfstatus(pictureAlbumReq.getShelfstatus());
        picturealbum.setExtinfo1(pictureAlbumReq.getExtinfo1());
        picturealbum.setExtinfo2(pictureAlbumReq.getExtinfo2());
        picturealbum.setPicturecover(pictureAlbumReq.getPicturecover());
        picturealbum.setContactpersonemail(pictureAlbumReq.getContactpersonemail());
        picturealbum.setContactpersonphone(pictureAlbumReq.getContactpersonphone());
        picturealbum.setContactpersonwechat(pictureAlbumReq.getContactpersonwechat());
        Calendar calendar = Calendar.getInstance();
        picturealbum.setExtinfo1(calendar.getTime().toString());//创建时间
        System.out.println("req: "+pictureAlbumReq.toString());
        System.out.println("picturealbum: "+picturealbum.toString());
        int flag;
        try {
            flag = picturealbumMapper.insert(picturealbum);
        }catch (Exception e){
           return WebResult.error("error when insert a new picture album into Database, "+e);
        }
        if(flag!=0){
            return WebResult.ok("successfully added a new picture album");
        }else{
            return WebResult.ok("failed to add a new picture album");
        }
    }

    public WebResult<?> editPictureAlbumById(PictureAlbumReq pictureAlbumReq) {
        Picturealbum picturealbum = new Picturealbum();
        picturealbum.setId(pictureAlbumReq.getId());
        picturealbum.setAlbumdescription(pictureAlbumReq.getAlbumdescription());
        picturealbum.setAlbumname(pictureAlbumReq.getAlbumname());
        picturealbum.setContactperson(pictureAlbumReq.getContactperson());
        picturealbum.setCreatoruser(pictureAlbumReq.getCreatoruser());
        picturealbum.setRelatedproducts(pictureAlbumReq.getRelatedproducts());
        picturealbum.setShelfmanagement(pictureAlbumReq.getShelfmanagement());
        picturealbum.setShelfstatus(pictureAlbumReq.getShelfstatus());
        picturealbum.setExtinfo1(pictureAlbumReq.getExtinfo1());
        picturealbum.setExtinfo2(pictureAlbumReq.getExtinfo2());
        picturealbum.setPicturecover(pictureAlbumReq.getPicturecover());
        picturealbum.setContactpersonemail(pictureAlbumReq.getContactpersonemail());
        picturealbum.setContactpersonphone(pictureAlbumReq.getContactpersonphone());
        picturealbum.setContactpersonwechat(pictureAlbumReq.getContactpersonwechat());
        Calendar calendar = Calendar.getInstance();
        picturealbum.setExtinfo2(calendar.getTime().toString());//修改时间
        int flag;
        try {
            flag = picturealbumMapper.updateByPrimaryKey(picturealbum);
        }catch (Exception e){
            return WebResult.error("error when update the picture album in Database, "+e);
        }
        if(flag!=0){
            return WebResult.ok("successfully edited a picture album");
        }else{
            return WebResult.error("failed to edit the picture album");
        }
    }

    public WebResult<?> editPictureAlbumByIdSelectively(PictureAlbumReq pictureAlbumReq) {
        Picturealbum picturealbum = new Picturealbum();
        picturealbum.setId(pictureAlbumReq.getId());
        picturealbum.setAlbumdescription(pictureAlbumReq.getAlbumdescription());
        picturealbum.setAlbumname(pictureAlbumReq.getAlbumname());
        picturealbum.setContactperson(pictureAlbumReq.getContactperson());
        picturealbum.setCreatoruser(pictureAlbumReq.getCreatoruser());
        picturealbum.setRelatedproducts(pictureAlbumReq.getRelatedproducts());
        picturealbum.setShelfmanagement(pictureAlbumReq.getShelfmanagement());
        picturealbum.setShelfstatus(pictureAlbumReq.getShelfstatus());
        picturealbum.setExtinfo1(pictureAlbumReq.getExtinfo1());
        picturealbum.setExtinfo2(pictureAlbumReq.getExtinfo2());
        picturealbum.setPicturecover(pictureAlbumReq.getPicturecover());
        picturealbum.setContactpersonemail(pictureAlbumReq.getContactpersonemail());
        picturealbum.setContactpersonphone(pictureAlbumReq.getContactpersonphone());
        picturealbum.setContactpersonwechat(pictureAlbumReq.getContactpersonwechat());
        Calendar calendar = Calendar.getInstance();
        picturealbum.setExtinfo2(calendar.getTime().toString());//修改时间
        int flag;
        try {
            flag = picturealbumMapper.updateByPrimaryKeySelective(picturealbum);
        }catch (Exception e){
            return WebResult.error("error when update the picture album in Database, "+e);
        }
        if(flag!=0){
            return WebResult.ok("successfully edited a picture album");
        }else{
            return WebResult.error("failed to edit the picture album");
        }
    }

    public WebResult<?> deletePictureAlbumById(PictureAlbumReq pictureAlbumReq) {
        int flag;
        try {
            flag = picturealbumMapper.deleteByPrimaryKey(pictureAlbumReq.getId());
        }catch (Exception e){
            return WebResult.error("error when delete the picture album into Database, "+e);
        }
        if(flag!=0){
            return WebResult.ok("successfully deleted the picture album");
        }else{
            return WebResult.error("failed to delete the picture album");
        }
    }

    public WebResult<?> queryPictureAlbumById(PictureAlbumReq pictureAlbumReq) {
        Picturealbum result;
        try {
            result = picturealbumMapper.selectByPrimaryKey(pictureAlbumReq.getId());
        }catch (Exception e){
            return WebResult.error("error when delete the picture album into Database, "+e);
        }
        if(result != null){
            return WebResult.ok(result);
        }else{
            return WebResult.error("failed to delete the picture album");
        }
    }

    public WebResult<?> queryPictureAlbumByPage(QueryPage queryPage) {

        PicturealbumExample example = new PicturealbumExample();
        example.setOrderByClause("id DESC");

        Page<Picturealbum> picturealbumsPage = picturealbumMapper.selectByExample(example, new RowBounds(queryPage.getPageNo(),queryPage.getPageSize()));
        //System.out.println(picturealbumsPage.toString());
        PageInfo<Picturealbum> picturealbumsPageInfo = picturealbumsPage.toPageInfo();
        //System.out.println(picturealbumsPageInfo.toString());

        return WebResult.ok(picturealbumsPageInfo);
    }
}
